/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.general.utils;

import gr.anagnosg.image.imagemagick.ParamElement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author anagnosg
 */
public class Parser {
    
    String value = null;
    String nextElement = null;
    int positionStart =0;
    int positionsEnd = 0 ;
    
    
    public static String NUMBER_PATTERN = "[\\d]+" ;
    public static String STRING_PATTERN = "[a-zA-Z]+" ;
    public static String SPECIAL_STRING_PATTERN = "[x%!<>]+" ;
    

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getNextElement() {
        return nextElement;
    }

    public void setNextElement(String nextElement) {
        this.nextElement = nextElement;
    }

    public int getPositionStart() {
        return positionStart;
    }

    public void setPositionStart(int positionStart) {
        this.positionStart = positionStart;
    }

    public int getPositionsEnd() {
        return positionsEnd;
    }

    public void setPositionsEnd(int positionsEnd) {
        this.positionsEnd = positionsEnd;
    }
    
    public Parser(){
    
    
    }
    
    public Parser(String value2Parse){
        this.value = value2Parse;
    }
    
    public ParamElement getNextParam(){
        ParamElement output = null;
        if(this.value!=null && this.value.length()>0){
            String nextChar = this.value.substring(0,1);
            if(isNumber(nextChar)){
                Pattern p = Pattern.compile(NUMBER_PATTERN);
                Matcher m = p.matcher(this.value); 
                if (m.find()) {
                    String valueT = m.group();
                     output = new ParamElement(valueT,ParamElement.TYPE_NUM);
                }
            }
            else if(Parser.isSpecialChar(nextChar) ){
                Pattern p = Pattern.compile(Parser.SPECIAL_STRING_PATTERN);
                Matcher m = p.matcher(this.value); 
                if (m.find()) {
                    String valueT = m.group();
                    int typeT = 0 ; 
                    if (valueT.length()==1){typeT=ParamElement.TYPE_SPECIAL_CHAR;}
                    if (valueT.length()>1){typeT=ParamElement.TYPE_SPECIAL_STRING;}
                    output = new ParamElement(valueT,typeT);
                }
            }
            else if(isString(nextChar)){
                Pattern p = Pattern.compile(Parser.STRING_PATTERN);
                Matcher m = p.matcher(this.value); 
                if (m.find()) {
                    String valueT = m.group();
                     output = new ParamElement(valueT,ParamElement.TYPE_STR);
                }
            }
            
            //remove from parser value the parameter.
            System.out.println("output.getValue()"+output.getValue()+" output.getValue().length()"+output.getValue().length()+"this.value.length() "+this.value.length());
            if(output.getValue().length() <= this.value.length()){
                this.value=  this.value.substring(output.getValue().length(), this.value.length());
            }
            else{
                this.value="";
            }
        }
        
        
        
        return output;
    }
    
    public static List findNums(String input ){
        List numbers = new ArrayList();    
        Pattern p = Pattern.compile(NUMBER_PATTERN);
        Matcher m = p.matcher(input); 
        while (m.find()) {
            numbers.add(m.group());
        }
        return numbers;
    }
    
    public static boolean isNumber(String value){
        boolean output = false;
        Pattern p = Pattern.compile(NUMBER_PATTERN);
        Matcher m = p.matcher(value); 
        if (m.find()) {
            String vt=m.group();
            if(value.equals(vt)){
                output=true;
            }
        }
        return output;
    }
    
    public static boolean isString(String value){
        boolean output = false;
        Pattern p = Pattern.compile(Parser.STRING_PATTERN);
        Matcher m = p.matcher(value); 
        if (m.find()) {
            String vt=m.group();
            if(value.equals(vt)){
                output=true;
            }
        }
        return output;
    }
    
    public static boolean isSpecialChar(String value){
        boolean output = false;
        Pattern p = Pattern.compile(Parser.SPECIAL_STRING_PATTERN);
        Matcher m = p.matcher(value); 
        if (m.find()) {
            String vt=m.group();
            if(value.equals(vt)){
                output=true;
            }
        }
        return output;
    }
    
    
    
    public static void main(String args[]){
        String testStr = "!x%";
        boolean res = isSpecialChar(testStr);
        System.out.println("res"+res);
        
    }
    
}
